package com.example.fan.airline_reservation.fragment_class;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fan.airline_reservation.AC_Interface;
import com.example.fan.airline_reservation.Adapter.FightAdapter;
import com.example.fan.airline_reservation.Model.Fight;
import com.example.fan.airline_reservation.Model.USER;
import com.example.fan.airline_reservation.R;
import com.example.fan.airline_reservation.Sign_in;
import com.example.fan.airline_reservation.Traveler_Interface;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobDate;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

public class ac_modify_fight extends Fragment implements View.OnClickListener,
        DatePickerDialog.OnDateSetListener{

    Button search;
    EditText eFrom,eTo;
    TextView cDate;
    Date date;
    int year,month,day;
    String from,to;

    /*RecyclerView*/
    private List<Fight> fightList=new ArrayList<>();
    RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_ac_modify,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        search=(Button)getActivity().findViewById(R.id.search);
        search.setOnClickListener(this);

        eFrom=(EditText)getActivity().findViewById(R.id.from);
        eTo=(EditText)getActivity().findViewById(R.id.to);

        cDate=(TextView)getActivity().findViewById(R.id.chooseDate);
        cDate.setOnClickListener(this);

        recyclerView=(RecyclerView)getActivity().findViewById(R.id.recycler_view);

    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        String desc = String.format("%d年%d月%d日", year, month + 1, dayOfMonth);
        this.year=year;
        this.month=month+1;
        this.day=dayOfMonth;
        cDate.setText(desc);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search:
                from=eFrom.getText().toString();
                to=eTo.getText().toString();
                if (from.length()!=0 && to.length()!=0) {
                    searchInTable(from, to, year, month, day);
                }else {
                    ToastShow("请输入正确的出发地和目的地");
                }
                break;
            case R.id.chooseDate:
                Calendar calendar=Calendar.getInstance();
                DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,
                        calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));
                dialog.show();
                break;
        }
    }

    public void searchInTable(String from,String to,int year,int month,int day){

        Date date=new Date(year-1900,month-1,day-1,23,59,59);
        Date date1=new Date(year-1900,month-1,day+1,0,0,0);

        long up=date.getTime();
        long down=date1.getTime();

        fightList.clear();

        String bq = "select * from Fight where FROM = '"+from+"' and TO = '"+to+"' "+
                "and DATE > "+up+" "+
                "and DATE < "+down+"";

        /*Log.d("test", bq);*/
        new BmobQuery<Fight>().doSQLQuery(bq,new SQLQueryListener<Fight>(){
            @Override
            public void done(BmobQueryResult<Fight> result, BmobException e) {
                if(e ==null){
                    List<Fight> list = (List<Fight>) result.getResults();
                    if(list!=null && list.size()>0){
                        for (int i=0;i<list.size();i++){
                            Fight f=new Fight(list.get(i).getFLIGHT(),
                                    list.get(i).getAIRCRAFT_MODEL(),
                                    list.get(i).getDATE(),
                                    list.get(i).getFROM(),
                                    list.get(i).getFROMNAME(),
                                    list.get(i).getTO(),
                                    list.get(i).getTONAME(),
                                    list.get(i).getPRICE(),
                                    list.get(i).getTODATE(),
                                    list.get(i).getCurrentPeole(),
                                    list.get(i).getCurrentState(),
                                    list.get(i).getDelayTime());
                            f.setObjectId(list.get(i).getObjectId());
                            fightList.add(f);
                        }
                        Log.d("test", list.size()+"");
                        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
                        recyclerView.setLayoutManager(layoutManager);
                        FightAdapter adapter=new FightAdapter(fightList);
                        recyclerView.setAdapter(adapter);
                        ToastShow("刷新成功! ");
                    }else{
                        ToastShow("暂无航班信息!");
                    }
                }else{
                    ToastShow("错误码："+e.getErrorCode()+"，错误描述："+e.getMessage());
                }
            }
        });
    }

    public void ToastShow(String message){
        Toast.makeText(getActivity(),message,Toast.LENGTH_SHORT).show();
    }
}
